﻿#region DaNotice
/*----------------------------------------------------------------
// 文件名：DaNotice.cs
// 功能描述：类Notice的数据访问层
//
// 
// 创建时间：2009-05-06
//
// 修改标识： 
// 修改描述： 
//----------------------------------------------------------------*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.Text;
using System.Xml;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
//using Microsoft.Practices.EnterpriseLibrary.Logging;
using EQT.Model; 
//using IDataLayer;

namespace EQT.Dal
{
    public	partial  class DaNotice   //可以继承更上层接口,INotice 
    {   
	
		//插入记录。要求实体对应的表必须有主键。
		//注意，若使用自增字段做主键，如SQL Server，则需要修改此处代码，将插入后得到的ID赋值给传入的entiry；
		//自动生成的版本假定主键不是自增数字。
		#region SQL const
		private const  string SQL_INSERT="INSERT INTO Notice (Notice_ID ,UID ,Publish_Type ,Notice_Content ,Create_Time ,Title ) VALUES "
			+"(@Notice_Id,@Uid,@Publish_Type,@Notice_Content,@Create_Time,@Title)";
		private const string SQL_UPDATE="UPDATE Notice SET UID=@UID, Publish_Type=@Publish_Type, Notice_Content=@Notice_Content, Create_Time=@Create_Time, Title=@Title WHERE Notice_ID=@Notice_ID";
		private const string SQL_DELETE="DELETE FROM Notice WHERE Notice_ID=@Notice_ID";
		private const string SQL_SELECT="SELECT Notice_ID ,UID ,Publish_Type ,Notice_Content ,Create_Time ,Title  from Notice  ";
		private const string SQL_SELECT_ONE="SELECT Notice_ID ,UID ,Publish_Type ,Notice_Content ,Create_Time ,Title  from Notice  WHERE Notice_ID=@Notice_ID";                              
		#endregion
		
        private Database db;
		
		#region Constructor
	 
		/// <summary>
        /// 默认构造函数，使用配置文件中默认的数据库配置。
        /// </summary>
		public DaNotice ()
		{ 
			db=DatabaseFactory.CreateDatabase();
		}
		/// <summary>
        /// 使用指定的databaseName数据库配置。
        /// </summary>
        /// <param name="databaseName">配置文件中数据库的配置名称。</param>
		public DaNotice (string databaseName)
		{ 
            this.db = DatabaseFactory.CreateDatabase(databaseName);
		} 
		
		public DaNotice (Database db)
		{ 
            this.db = db;
		} 
		
		#endregion
		
		//以下是CRUD方法，每个方法都有引入外部事务的版本

	    #region Add
		
		///build the command object.It never throw exception.
		private DbCommand ConstructAddCommand()
		{
			DbCommand dbCommand = db.GetSqlStringCommand(SQL_INSERT);
			 
			//设置参数:Notice_Id	
			db.AddInParameter(dbCommand,"Notice_Id",DbType.AnsiString);
			//设置参数:Uid	
			db.AddInParameter(dbCommand,"Uid",DbType.AnsiString);
			//设置参数:Publish_Type	
			db.AddInParameter(dbCommand,"Publish_Type",DbType.Int32);
			//设置参数:Notice_Content	
			db.AddInParameter(dbCommand,"Notice_Content",DbType.String);
			//设置参数:Create_Time	
			db.AddInParameter(dbCommand,"Create_Time",DbType.DateTime);
			//设置参数:Title	
			db.AddInParameter(dbCommand,"Title",DbType.String);
			return dbCommand;
		}		
		private DbCommand ConstructAddCommand(MoNotice entity)
		{
			DbCommand dbCommand=db.GetSqlStringCommand(SQL_INSERT);
			 
			//设置参数:Notice_Id	
			db.AddInParameter(dbCommand,"Notice_Id",DbType.AnsiString,entity.Notice_Id);
			//设置参数:Uid	
			db.AddInParameter(dbCommand,"Uid",DbType.AnsiString,entity.Uid);
			//设置参数:Publish_Type	
			db.AddInParameter(dbCommand,"Publish_Type",DbType.Int32,entity.Publish_Type);
			//设置参数:Notice_Content	
			db.AddInParameter(dbCommand,"Notice_Content",DbType.String,entity.Notice_Content);
			//设置参数:Create_Time	
			db.AddInParameter(dbCommand,"Create_Time",DbType.DateTime,entity.Create_Time);
			//设置参数:Title	
			db.AddInParameter(dbCommand,"Title",DbType.String,entity.Title);
			 
			return dbCommand;
		}
	
	    /// <summary>
		///在某事务下增加一条记录，事务没有commit
		/// </summary>
	    public int Add(MoNotice entity,DbTransaction trans)
	    {	try
            {
				DbCommand dbCommand=ConstructAddCommand(entity);			
				return db.ExecuteNonQuery(dbCommand,trans);
			}
			catch(DbException ex)
			{				
				HandleDbException(ex,entity);				
			}
			return 0;
			
	    }		
	    public int Add(MoNotice entity) 
        {	
			try
            {
				DbCommand dbCommand=ConstructAddCommand(entity);			
				return db.ExecuteNonQuery(dbCommand);
			}
			catch(DbException ex)
			{				
				HandleDbException(ex,entity);		
			}
			return 0;
	    }
		
		public int Add(List<MoNotice> entity,DbTransaction trans)
		{
			int rc=0;		
            DbCommand dbCommand=ConstructAddCommand();
			int j=0;
			try
			{
			   for(j=0;j<entity.Count;j++)
			   {	
				//设置参数:Notice_Id	
				db.SetParameterValue(dbCommand,"Notice_Id",entity[j].Notice_Id);
				//设置参数:Uid	
				db.SetParameterValue(dbCommand,"Uid",entity[j].Uid);
				//设置参数:Publish_Type	
				db.SetParameterValue(dbCommand,"Publish_Type",entity[j].Publish_Type);
				//设置参数:Notice_Content	
				db.SetParameterValue(dbCommand,"Notice_Content",entity[j].Notice_Content);
				//设置参数:Create_Time	
				db.SetParameterValue(dbCommand,"Create_Time",entity[j].Create_Time);
				//设置参数:Title	
				db.SetParameterValue(dbCommand,"Title",entity[j].Title);
				rc+=db.ExecuteNonQuery(dbCommand,trans);
			   }
			}
			catch(DbException ex)
			{
				HandleDbException(ex,entity[j]);				
			}			 
            return rc;
		}		
		public int Add(List<MoNotice> entity)
		{
			int rc = 0;			
			using (DbConnection cnn = db.CreateConnection())
            {    
				DbTransaction trans=null;
				try
				{
					cnn.Open();
					using(trans = cnn.BeginTransaction())
					{
						rc=Add(entity,trans);//This function throw customer exception.
						trans.Commit();
					}
					cnn.Close();
				}
				catch(DbException ex)
				{
					if(trans!=null)trans.Rollback();		
					HandleDbException(ex);	
				}
				///TODO:catch customer exception.
				//catch
				//{
				//	
				//}
			}
			return rc;
		}
		
        public int Add(DataTable dt, DbTransaction trans)
		{
			int rc=0;		 
            DbCommand dbCommand =ConstructAddCommand();	
			int j=0;
			try
			{
			   for(j=0;j<dt.Rows.Count;j++)
			   {	
					//设置参数:Notice_Id	
					db.SetParameterValue(dbCommand,"Notice_Id",dt.Rows[j]["Notice_Id"]);
					//设置参数:Uid	
					db.SetParameterValue(dbCommand,"Uid",dt.Rows[j]["Uid"]);
					//设置参数:Publish_Type	
					db.SetParameterValue(dbCommand,"Publish_Type",dt.Rows[j]["Publish_Type"]);
					//设置参数:Notice_Content	
					db.SetParameterValue(dbCommand,"Notice_Content",dt.Rows[j]["Notice_Content"]);
					//设置参数:Create_Time	
					db.SetParameterValue(dbCommand,"Create_Time",dt.Rows[j]["Create_Time"]);
					//设置参数:Title	
					db.SetParameterValue(dbCommand,"Title",dt.Rows[j]["Title"]);
					rc+=db.ExecuteNonQuery(dbCommand,trans);
			   }
			}
			catch(DbException ex)
			{				
				HandleDbException(ex,dt.Rows[j]);				
			}	
			 
            return rc;
		}		
		public int Add(DataTable dt)
        {
			int rc=0;
            using (DbConnection cnn = db.CreateConnection())
            { 
				DbTransaction trans=null;
				try
                {
					cnn.Open();
					using(trans = cnn.BeginTransaction())
					{
						rc = Add(dt, trans);
						trans.Commit();
						cnn.Close();
					}
				}
				catch(DbException ex)
				{	
					if(trans!=null)trans.Rollback();
					HandleDbException(ex);	
				}	
				///TODO:catch customer exception.
				//catch
				//{
				//	
				//}
            }
            return rc;
        }
		#endregion
		
		#region Update
		private DbCommand ConstructUpdateCommand()
		{
			DbCommand dbCommand=db.GetSqlStringCommand(SQL_UPDATE);
		    
			db.AddInParameter(dbCommand,"Uid",DbType.AnsiString);
			db.AddInParameter(dbCommand,"Publish_Type",DbType.Int32);
			db.AddInParameter(dbCommand,"Notice_Content",DbType.String);
			db.AddInParameter(dbCommand,"Create_Time",DbType.DateTime);
			db.AddInParameter(dbCommand,"Title",DbType.String);
		 
			db.AddInParameter(dbCommand,"Notice_Id",DbType.AnsiString);
			 
			return dbCommand;
		}
		private DbCommand ConstructUpdateCommand(MoNotice entity)
		{
			DbCommand dbCommand=db.GetSqlStringCommand(SQL_UPDATE);
		    
			db.AddInParameter(dbCommand,"Uid",DbType.AnsiString,entity.Uid);
			db.AddInParameter(dbCommand,"Publish_Type",DbType.Int32,entity.Publish_Type);
			db.AddInParameter(dbCommand,"Notice_Content",DbType.String,entity.Notice_Content);
			db.AddInParameter(dbCommand,"Create_Time",DbType.DateTime,entity.Create_Time);
			db.AddInParameter(dbCommand,"Title",DbType.String,entity.Title);
		 
			db.AddInParameter(dbCommand,"Notice_Id",DbType.AnsiString,entity.Notice_Id);
			 
			return dbCommand;
		}
		/// <summary>
		/// 根据主键更新实体。注意:主键列本身并未更新。
		/// </summary>	
	    public int Update(MoNotice entity,DbTransaction trans)
	    {	    			
		    try
            {			
				DbCommand dbCommand=ConstructUpdateCommand(entity);
				return db.ExecuteNonQuery(dbCommand,trans);
			}
			catch(DbException ex)
			{				
				HandleDbException(ex,entity);		
			}
			return 0;
		}		
		public int Update(MoNotice entity)
		{
			try
            {			
				DbCommand dbCommand=ConstructUpdateCommand(entity);
				return db.ExecuteNonQuery(dbCommand);
			}
			catch(DbException ex)
			{				
				HandleDbException(ex,entity);		
			}
			return 0;			
		}		
		public int Update(List<MoNotice> entity,DbTransaction trans)
		{
			int rc=0;			 
            DbCommand dbCommand=ConstructUpdateCommand();
			int j=0;
			try
			{
			    for(j=0;j<entity.Count;j++)
				{	
					db.SetParameterValue(dbCommand,"Notice_Id",entity[j].Notice_Id);
					db.SetParameterValue(dbCommand,"Uid",entity[j].Uid);
					db.SetParameterValue(dbCommand,"Publish_Type",entity[j].Publish_Type);
					db.SetParameterValue(dbCommand,"Notice_Content",entity[j].Notice_Content);
					db.SetParameterValue(dbCommand,"Create_Time",entity[j].Create_Time);
					db.SetParameterValue(dbCommand,"Title",entity[j].Title);
					rc+=db.ExecuteNonQuery(dbCommand,trans);
				}
			}
			catch(DbException ex)
			{				
				HandleDbException(ex,entity[j]);		
			}			 
			return rc;			
		}		
		public int Update(List<MoNotice> entity)
		{
			int rc = 0;	
			using (DbConnection cnn = db.CreateConnection())
            {   
				DbTransaction trans=null;
				try
				{          
					cnn.Open();
					using(trans = cnn.BeginTransaction())
					{
					rc=Update(entity,trans);
					trans.Commit();		
					}
					cnn.Close();
				}
				catch(DbException ex)
				{
					if(trans!=null)trans.Rollback();	
					HandleDbException(ex);	
				}				
			}
			return rc;
		}		
		public int Update(DataTable dt, DbTransaction trans)
		{
			int rc=0;			 
            DbCommand dbCommand=ConstructUpdateCommand();		
			int j=0;
		    try
		    {
			    for(j=0;j<dt.Rows.Count;j++)
				{	
					db.SetParameterValue(dbCommand,"Notice_Id",dt.Rows[j]["Notice_Id"]);
					db.SetParameterValue(dbCommand,"Uid",dt.Rows[j]["Uid"]);
					db.SetParameterValue(dbCommand,"Publish_Type",dt.Rows[j]["Publish_Type"]);
					db.SetParameterValue(dbCommand,"Notice_Content",dt.Rows[j]["Notice_Content"]);
					db.SetParameterValue(dbCommand,"Create_Time",dt.Rows[j]["Create_Time"]);
					db.SetParameterValue(dbCommand,"Title",dt.Rows[j]["Title"]);
					rc+=db.ExecuteNonQuery(dbCommand,trans);
				}
			}
			catch(DbException ex)
			{				
				HandleDbException(ex,dt.Rows[j]);		
			}		
            return rc;
			
		}		
		public int Update(DataTable dt)
        {
			if(dt==null ||dt.Rows.Count==0) return 0;
            int rc=0;       
            using (DbConnection cnn = db.CreateConnection())
            {
				DbTransaction trans=null;
				try
                {
					cnn.Open();
					using(trans = cnn.BeginTransaction())
					{
						rc = Add(dt, trans);
						trans.Commit();						
					}   
					cnn.Close();                  
                }
                catch(DbException ex)
				{
					if(trans!=null)trans.Rollback();	
					HandleDbException(ex);	
				}		
                
            }
            return rc;
        }
		#endregion
		
		#region Update By DataSet
		private void PrepareDataAdapterCommand(out DbCommand dbInsertCommand,
		                     out DbCommand dbUpdateCommand,out DbCommand dbDeleteCommand)
		{
			dbInsertCommand = db.GetSqlStringCommand(SQL_INSERT);
			#region set insert cmd parameters
			db.AddInParameter(dbInsertCommand, "Notice_Id",DbType.AnsiString, "Notice_Id", DataRowVersion.Current);
			db.AddInParameter(dbInsertCommand, "Uid",DbType.AnsiString, "Uid", DataRowVersion.Current);
			db.AddInParameter(dbInsertCommand, "Publish_Type",DbType.Int32, "Publish_Type", DataRowVersion.Current);
			db.AddInParameter(dbInsertCommand, "Notice_Content",DbType.String, "Notice_Content", DataRowVersion.Current);
			db.AddInParameter(dbInsertCommand, "Create_Time",DbType.DateTime, "Create_Time", DataRowVersion.Current);
			db.AddInParameter(dbInsertCommand, "Title",DbType.String, "Title", DataRowVersion.Current);
			#endregion
			dbUpdateCommand = db.GetSqlStringCommand(SQL_UPDATE);
			#region Set update cmd value parameters
			db.AddInParameter(dbUpdateCommand, "Uid", DbType.AnsiString, "Uid", DataRowVersion.Current);
			db.AddInParameter(dbUpdateCommand, "Publish_Type", DbType.Int32, "Publish_Type", DataRowVersion.Current);
			db.AddInParameter(dbUpdateCommand, "Notice_Content", DbType.String, "Notice_Content", DataRowVersion.Current);
			db.AddInParameter(dbUpdateCommand, "Create_Time", DbType.DateTime, "Create_Time", DataRowVersion.Current);
			db.AddInParameter(dbUpdateCommand, "Title", DbType.String, "Title", DataRowVersion.Current);
			#endregion
			#region set update cmd pk where parameters
			db.AddInParameter(dbUpdateCommand, "Notice_Id", DbType.AnsiString, "Notice_Id", DataRowVersion.Current);
			#endregion			
			dbDeleteCommand = db.GetSqlStringCommand(SQL_DELETE);
			#region set delete cmd pk where parameters
			db.AddInParameter(dbDeleteCommand, "Notice_Id", DbType.AnsiString, "Notice_Id", DataRowVersion.Current);
			#endregion
		}
		
		
		
		/// <summary>
		/// 通过DataSet修改表数据（带事务）
		/// </summary>
		public int UpdateByDataSet(DataSet dst, string strTableName, DbTransaction trans)
		{
			DbCommand dbInsertCommand = null;			
			DbCommand dbUpdateCommand = null;
			DbCommand dbDeleteCommand = null;
			PrepareDataAdapterCommand(out dbInsertCommand,out dbUpdateCommand,out dbDeleteCommand);
			try
            {
			    return db.UpdateDataSet(dst, strTableName, dbInsertCommand, dbUpdateCommand, dbDeleteCommand, trans);
			}
			catch(DbException ex)
			{			
				trans.Rollback();
				HandleDbException(ex);				
			}
			return 0;//TODO:Should return Application Specific Error Code.
		}
		
		/// <summary>
		/// 通过DataSet修改表数据（不带事务）
		/// </summary>
		public int UpdateByDataSet(DataSet dst, string strTableName)
		{
			DbCommand dbInsertCommand = null;			
			DbCommand dbUpdateCommand = null;
			DbCommand dbDeleteCommand = null;
			PrepareDataAdapterCommand(out dbInsertCommand,out dbUpdateCommand,out dbDeleteCommand);
			try
            {
			    return db.UpdateDataSet(dst, strTableName, dbInsertCommand, dbUpdateCommand, dbDeleteCommand, UpdateBehavior.Transactional);			
			}
			catch(DbException ex)
			{	
				HandleDbException(ex);				
			}
			return 0;//TODO:Should return Application Specific Error Code.			
		}
		
		
		
		#endregion 
		
		#region Delete
		
		private DbCommand ConstructDeleteCommand()
		{
			DbCommand dbCommand=db.GetSqlStringCommand(SQL_DELETE);
			 
			db.AddInParameter(dbCommand,"Notice_Id",DbType.AnsiString);
			 
			return dbCommand;
		}		
		private DbCommand ConstructDeleteCommand(MoNotice entity)
		{
			DbCommand dbCommand=db.GetSqlStringCommand(SQL_DELETE);
			 
			db.AddInParameter(dbCommand,"Notice_Id",DbType.AnsiString,entity.Notice_Id);
		 
			return dbCommand;
		}
		
		/// <summary>
		/// delete by pk(entity)
		/// </summary>	
		public int Delete(MoNotice entity,DbTransaction trans)
		{	try
            {
				DbCommand dbCommand=ConstructDeleteCommand(entity);			
				return db.ExecuteNonQuery(dbCommand,trans);
			}
			catch(DbException ex)
			{			
				trans.Rollback();
				HandleDbException(ex,entity);				
			}
			return 0;
		}	
		public int Delete(MoNotice entity)
		{
			try
            {
				DbCommand dbCommand=ConstructDeleteCommand(entity);			
				return db.ExecuteNonQuery(dbCommand);
			}
			catch(DbException ex)
			{				
				HandleDbException(ex,entity);				
			}
			return 0;
		}		
		/// <summary>
		/// mul-delete
		/// </summary>	
		public int Delete(List<MoNotice> entities,DbTransaction trans)
		{
			int rc=0;	
			int j=0;
			DbCommand dbCommand=ConstructDeleteCommand();		
			try
			{
			    for(j=0;j<entities.Count;j++)
				{	
					db.SetParameterValue(dbCommand,"Notice_Id",entities[j].Notice_Id);
					rc+=db.ExecuteNonQuery(dbCommand,trans);
				}
			}
			catch(DbException ex)
			{	
				if(trans!=null)trans.Rollback();
				HandleDbException(ex,entities[j]);				
			}			 
			return rc;			
		}		
		public int Delete(List<MoNotice> entities)
		{
			int rc = 0;			
			using (DbConnection cnn = db.CreateConnection())
            {      
				DbTransaction trans=null;
				try
				{
					cnn.Open();
					using(trans = cnn.BeginTransaction())
					{
						rc=Delete(entities,trans);
						trans.Commit();
					}               
					cnn.Close();
				}
				catch(DbException ex)
				{	
					if(trans!=null)trans.Rollback();
					HandleDbException(ex);				
				}	
			}
			return rc;
		}
		
		
		public int Delete(DataTable dt,DbTransaction trans)
		{
			int rc=0;			
			int j=0;
			DbCommand dbCommand=ConstructDeleteCommand();		
			try
			{
			    for(j=0;j<dt.Rows.Count;j++)
				{	
					db.SetParameterValue(dbCommand,"Notice_Id",dt.Rows[j]["Notice_Id"]);
					rc+=db.ExecuteNonQuery(dbCommand,trans);
				}
			}
			catch(DbException ex)
			{	
				if(trans!=null)trans.Rollback();
				HandleDbException(ex,dt.Rows[j]);				
			}			 
			return rc;			
		 	
		}		
		public int Delete(DataTable dt)
		{
			int rc=0;
		    using (DbConnection cnn = db.CreateConnection())
            {
				DbTransaction trans=null;
				try
				{
					cnn.Open();				
					using(trans = cnn.BeginTransaction())
					{
						rc = Delete(dt, trans);
						trans.Commit();
					}
					cnn.Close();
                }
                catch(DbException ex)
				{	
					if(trans!=null)trans.Rollback();
					HandleDbException(ex);				
				}
                
            }
            return rc;
		}		
		/// <summary>
		/// 根据主键列删除记录。
		/// </summary>	
		public int Delete(string Notice_Id,DbTransaction trans)
		{			 
			try
			{
				DbCommand dbCommand=db.GetSqlStringCommand(SQL_DELETE);
				 
				db.AddInParameter(dbCommand,"Notice_Id",DbType.AnsiString,Notice_Id);
				 
				return db.ExecuteNonQuery(dbCommand,trans);
			}
			catch(DbException ex)
			{	
				if(trans!=null)trans.Rollback();
				HandleDbException(ex);				
			}	
			return 0;
		}	
		
		public int Delete(string Notice_Id)
		{
			try
			{
				DbCommand dbCommand=db.GetSqlStringCommand(SQL_DELETE);
				 
				db.AddInParameter(dbCommand,"Notice_Id",DbType.AnsiString,Notice_Id);
				 
				return db.ExecuteNonQuery(dbCommand);
			}
			catch(DbException ex)
			{			 
				HandleDbException(ex);				
			}	
			return -1;
		}
		#endregion
		
		#region Query All by condition
		/// <summary>
		/// 返回满足WHERE条件的记录的集合,若condition为空，则查询所有
		/// </summary>		
		public DataSet Query(string condition)
		{			
			try
			{
				return db.ExecuteDataSet(CommandType.Text,String.IsNullOrEmpty(condition)?SQL_SELECT:SQL_SELECT+condition);	 
			}
			catch(DbException ex)
			{			 
				HandleDbException(ex);				
			}	
            return new DataSet();
		}		
	    public DataSet Query(string condition, DbTransaction trans)
        {   
            try
            {
                return db.ExecuteDataSet(trans, CommandType.Text, String.IsNullOrEmpty(condition) ? SQL_SELECT : SQL_SELECT + condition);
            }
            catch(DbException ex)
			{			 
				HandleDbException(ex);				
			}	
            return new DataSet();
        }
		#endregion
		
		#region GetEntity(s)
		/// <summary>
		///根据主键返回一个实体对象
		/// </summary>
	    public MoNotice GetEntity(string mNotice_Id )
		{			 
			MoNotice entity = new MoNotice();
			entity.Notice_Id=mNotice_Id;
			if(GetEntityEx(entity))
			{
				return entity;
			}
			else
			{
				return null;
			}
		}
		
		public MoNotice GetEntity(string mNotice_Id ,DbTransaction trans)
		{			 
			MoNotice entity = new MoNotice();
			entity.Notice_Id=mNotice_Id;
			if(GetEntityEx(entity,trans))
			{
				return entity;
			}
			else
			{
				return null;
			}
		}
		
		private DbCommand ConstructSelectOneCommand(Database db,MoNotice entity)
		{
			DbCommand dbCommand=db.GetSqlStringCommand(SQL_SELECT_ONE);
			 
			db.AddInParameter(dbCommand,"Notice_Id",DbType.AnsiString,entity.Notice_Id);
			return dbCommand;
		}
		private DbCommand ConstructSelectOneCommand(Database db)
		{
			DbCommand dbCommand=db.GetSqlStringCommand(SQL_SELECT_ONE);
			 
			db.AddInParameter(dbCommand,"Notice_Id",DbType.AnsiString);
			return dbCommand;
		}
		private void PopulateEntityByDataReader(IDataReader reader,ref MoNotice entity)
		{
			if (!reader.IsDBNull(0)) entity.Notice_Id = reader.GetString(0);
			if (!reader.IsDBNull(1)) entity.Uid = reader.GetString(1);
			if (!reader.IsDBNull(2)) entity.Publish_Type = reader.GetInt32(2);
			if (!reader.IsDBNull(3)) entity.Notice_Content = reader.GetString(3);
			if (!reader.IsDBNull(4)) entity.Create_Time = reader.GetDateTime(4);
			if (!reader.IsDBNull(5)) entity.Title = reader.GetString(5);
		}
		
		/// <summary>
		///实体的主键已经赋值。需要注意的是，实体应该中是一个类，而不是结构.
		///如果实体是结构，为了避免复制对象，应考虑使用ref关键字。
		/// </summary>
		public bool GetEntityEx(MoNotice entity)
		{
			DbCommand dbCommand=ConstructSelectOneCommand(db,entity);
			try
			{
				using(IDataReader reader=db.ExecuteReader(dbCommand))
				{   
					if(reader.Read()) 
					{
						PopulateEntityByDataReader(reader,ref entity);
						return true;
					}
				}
			}
			catch(DbException ex)
			{	 
				HandleDbException(ex,entity);				
			}	
			return false;
		}
		public bool GetEntityEx(MoNotice entity,DbTransaction trans)
		{		
			DbCommand dbCommand=ConstructSelectOneCommand(db,entity);
			try
			{
				using(IDataReader reader=db.ExecuteReader(dbCommand,trans))
				{   
					if(reader.Read())
					{
						PopulateEntityByDataReader(reader,ref entity);	
						return true;
					}
				}
			} 
			catch(DbException ex)
			{			 
				trans.Rollback();
				HandleDbException(ex,entity);				
			}	
			return false;
			
		}
		
		public List<MoNotice> GetEntities(string condition,DbTransaction trans)
		{
			List<MoNotice> list=new List<MoNotice>();			
			try
			{				
			    using(IDataReader reader= db.ExecuteReader(trans,CommandType.Text, String.IsNullOrEmpty(condition) ? SQL_SELECT : SQL_SELECT+ condition))
			    {
				    while(reader.Read())
					{			
						MoNotice entity=new MoNotice();
						PopulateEntityByDataReader(reader,ref entity);	
						list.Add(entity);
					}
			    }
			}
			catch(DbException ex)
			{			 
				trans.Rollback();
				HandleDbException(ex);				
			}	
			return list;
			
		}
		
		
		public List<MoNotice> GetEntities(string condition)
		{
			List<MoNotice> list=new List<MoNotice>();	
			try
			{
				using(IDataReader reader= db.ExecuteReader(CommandType.Text, String.IsNullOrEmpty(condition) ? SQL_SELECT : SQL_SELECT+" " + condition))
				{   
					while(reader.Read())
					{	
						MoNotice entity=new MoNotice();
						PopulateEntityByDataReader(reader,ref entity);	
						list.Add(entity);
					}
				}
			}
			catch(DbException ex)
			{	 
				HandleDbException(ex);				
			}	
			return list;
		}
		#endregion
		
		#region Count
		/// <summary>
		/// 返回表的记录总数
		/// </summary>
		public int GetCount()
		{
			return Convert.ToInt32(db.ExecuteScalar(CommandType.Text,
			"SELECT COUNT(*) FROM Notice "));
		}
		
		public int GetCount(DbTransaction trans)
		{
			try
			{
			    return Convert.ToInt32(db.ExecuteScalar(trans,CommandType.Text,
			    "SELECT COUNT(*) FROM Notice "));
		    }
		    catch(DbException ex)
			{	 
				HandleDbException(ex);				
			}	
			return -1;
		}
		
		/// <summary>
		/// 返回满足WHERE条件记录数
		/// </summary>
		public int GetCount(string condition)
		{
			return Convert.ToInt32(db.ExecuteScalar(CommandType.Text,String.IsNullOrEmpty(condition)?
			"SELECT COUNT(*) FROM Notice ":"SELECT COUNT(*) FROM Notice "+condition ));
		}
		
		public int GetCount(string condition,DbTransaction trans)
		{
			try
			{
			   return Convert.ToInt32(db.ExecuteScalar(trans,CommandType.Text,String.IsNullOrEmpty(condition)?
			   "SELECT COUNT(*) FROM Notice ":"SELECT COUNT(*) FROM Notice "+condition ));
		    }
			catch(DbException ex)
			{	 
				HandleDbException(ex);				
			}	
			return -1;
		}
		#endregion
		
		#region Check isExist
		/// <summary>
		/// 判断是否存在此主键的记录
		/// </summary>
        public bool IsExist(string mNotice_Id )
		{
			string sql="SELECT COUNT(*) FROM Notice  WHERE Notice_ID=@Notice_Id  ";
			
			DbCommand dbCommand=db.GetSqlStringCommand(sql);
			db.AddInParameter(dbCommand,"Notice_Id",DbType.AnsiString,mNotice_Id);
			return Convert.ToInt32(db.ExecuteScalar(dbCommand))>0;
		}
		
		public bool IsExist(string mNotice_Id ,DbTransaction trans)
		{			 
			string sql="select count(*) from Notice  where Notice_ID=@Notice_Id  ";
			
			DbCommand dbCommand=db.GetSqlStringCommand(sql);
			db.AddInParameter(dbCommand,"Notice_Id",DbType.AnsiString,mNotice_Id);
			try
			{
			   return Convert.ToInt32(db.ExecuteScalar(dbCommand,trans))>0;
			}
			catch
			{
			    trans.Rollback();
				throw;
			}		 
		}
		
		public bool IsExist(MoNotice entity,DbTransaction trans)
		{			
			string sql="select count(*) from Notice  where Notice_ID=@Notice_Id  ";
			
			DbCommand dbCommand=db.GetSqlStringCommand(sql);
			#region set pk where parameters
			db.AddInParameter(dbCommand,"Notice_Id",DbType.AnsiString,entity.Notice_Id);
			#endregion
			try
			{
			   return Convert.ToInt32(db.ExecuteScalar(dbCommand,trans))>0;
			}
			catch
			{
			    trans.Rollback();
				throw;
			}		 
		}
		
		public bool IsExist(MoNotice entity)
		{
			string sql="select count(*) from Notice  where Notice_ID=@Notice_Id  ";
			
			DbCommand dbCommand=db.GetSqlStringCommand(sql);
			#region set pk where parameters
			db.AddInParameter(dbCommand,"Notice_Id",DbType.AnsiString,entity.Notice_Id);
			#endregion
			 
			return Convert.ToInt32(db.ExecuteScalar(dbCommand))>0;
			 	 
		}
		#endregion
		
		#region Handle Exception
		private void HandleDbException(DbException ex)
		{
			//TODO:handle db exception.
			//Please throw application custom exception.
			
			throw ex;
		}
		private void HandleDbException(DbException ex, MoNotice entity)
		{			
			//NOTE:If you use SQL Server/Oracle/Db2... ,Please add the [internal static class ErrorCodes]
			//Use the ErrorCodes like below:
			//if (ex.Number == ErrorCodes.SqlUserRaisedError)
            //{
            //    switch (ex.State)
            //    {
            //    case ErrorCodes.ValidationError:
            //        string[] messageParts = ex.Errors[0].Message.Split(':');
            //        throw new RepositoryValidationException(
            //            mapper.MapDbParameterToBusinessEntityProperty(messageParts[0]),
            //            messageParts[1], ex);

            //    case ErrorCodes.ConcurrencyViolationError:
            //        throw new ConcurrencyViolationException(ex.Message, ex);

            //    }
            //}
			
            //TODO:Please contruct your customer Exception.
            //throw new RepositoryFailureException(ex);      
			throw ex;           
		}
		private void HandleDbException(DbException ex,DataRow entity)
		{
			//TODO:If you use SQL Server/Oracle/Db2... ,Please add the [internal static class ErrorCodes]
			//if (ex.Number == ErrorCodes.SqlUserRaisedError)
            //{
            //    switch (ex.State)
            //    {
            //    case ErrorCodes.ValidationError:
            //        string[] messageParts = ex.Errors[0].Message.Split(':');
            //        throw new RepositoryValidationException(
            //            mapper.MapDbParameterToBusinessEntityProperty(messageParts[0]),
            //            messageParts[1], ex);

            //    case ErrorCodes.ConcurrencyViolationError:
            //        throw new ConcurrencyViolationException(ex.Message, ex);

            //    }
            //}
			
            //TODO:Please contruct your customer Exception.
            //throw new RepositoryFailureException(ex);      
			throw ex;           
		}
		#endregion
	}
}	
#endregion

